﻿using System;
using fastCSharp.code.cSharp;
using fastCSharp;
using System.Collections.Generic;

namespace diantou.web
{
    /// <summary>
    /// 分页信息
    /// </summary>
    [fastCSharp.code.cSharp.webView.clientType(Name = "diantou.page", MemberName = null)]
    struct pageInfo
    {
        /// <summary>
        /// 当前页面URL
        /// </summary>
        [fastCSharp.code.cSharp.webView.outputAjax]
        public string Path;
        /// <summary>
        /// 分页尺寸
        /// </summary>
        [fastCSharp.code.cSharp.webView.outputAjax]
        public int Size;
        /// <summary>
        /// 当前分页号
        /// </summary>
        [fastCSharp.code.cSharp.webView.outputAjax]
        public int Page;
        /// <summary>
        /// 数据集合总数
        /// </summary>
        [fastCSharp.code.cSharp.webView.outputAjax]
        public int Count;
        /// <summary>
        /// 分页数量
        /// </summary>
        [fastCSharp.code.ignore]
        private int? pageCount;
        /// <summary>
        /// 分页数量
        /// </summary>
        [fastCSharp.code.ignore]
        public int PageCount
        {
            get
            {
                if (pageCount == null)
                {
                    int count = (Count + Size - 1) / Size;
                    pageCount = count;
                    if (Page > 1)
                    {
                        if (Page > count) Page = (int)count == 0 ? 1 : (int)count;
                    }
                    else Page = 1;
                    startPage = Page - 5;
                    endPage = Page + 5;
                    if (startPage <= 0) endPage -= startPage;
                    if (endPage >= count)
                    {
                        startPage -= endPage - count;
                        endPage = count;
                    }
                    if (startPage <= 0) startPage = 1;
                }
                return (int)pageCount;
            }
        }
        /// <summary>
        /// 起始分页
        /// </summary>
        [fastCSharp.code.ignore]
        private int startPage;
        /// <summary>
        /// 结束分页
        /// </summary>
        [fastCSharp.code.ignore]
        private int endPage;
        /// <summary>
        /// 分页集合
        /// </summary>
        [fastCSharp.code.ignore]
        public IEnumerable<int> Pages
        {
            get
            {
                while (startPage <= endPage) yield return ++startPage;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="size"></param>
        /// <param name="count"></param>
        public void Set(int size, int count)
        {
            Size = size;
            Count = count;
        }
    }
}
